package com.example.element.module.service

import android.app.*
import android.content.Context
import android.content.Intent
import android.graphics.BitmapFactory
import android.os.Build
import android.os.IBinder
import androidx.core.app.NotificationCompat
import com.example.common.utils.L
import com.example.element.R
import com.example.element.activity.MenuActivity

/**
 * 前台Service
 */
class ForegroundService : Service() {

    private val TAG = "ForegroundService"
    override fun onCreate() {
        super.onCreate()
        L.e(TAG, "onCreate")
        val manager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val channel = NotificationChannel("ForegroundService", "Foreground Service notification", NotificationManager.IMPORTANCE_DEFAULT)
            manager.createNotificationChannel(channel)
        }

        val intent = Intent(this, MenuActivity::class.java)
        val pi = PendingIntent.getActivity(this, 0, intent, 0)
        val notification = NotificationCompat.Builder(this, "ForegroundService")
                .setContentTitle("This is content title")
                .setContentText("This is content text")
                .setSmallIcon(R.mipmap.launcher_icon)
                .setLargeIcon(BitmapFactory.decodeResource(resources,R.mipmap.launcher_icon))
                .setContentIntent(pi)
                .build()
        startForeground(1, notification)
    }

    override fun onBind(intent: Intent): IBinder {
        TODO("Return the communication channel to the service.")
    }
}
